import { _decorator, Component, Node } from 'cc';
import { BaseView } from '../../rxjs/cc3/BaseView';
import { screen } from 'cc';
import { UITransform } from 'cc';
import { gm } from '../../../managers/GameManager';
import { Enum } from 'cc';
import { v3 } from 'cc';
import { size } from 'cc';
import { CocosTools } from '../CocosTools';
import { ReactiveNumber } from '../../rxjs/cc3/ReactiveNumber';
import { Sprite } from 'cc';
import { EDITOR } from 'cc/env';
import { view } from 'cc';
import { StringTools } from '../StringTools';
const { ccclass, property, executeInEditMode } = _decorator;

// declare const CC_EDITOR: boolean;

export enum ResizeMode {
    Stretch, // 拉伸 使用setContentSize,改变长宽比恰好填充整个屏幕,使用时需要保持scale为1
    Crop, // 裁切 使用scale,等比填充整个屏幕,使用时,contentsize提前保持比例,比如直接使用SizeMode为RAW
    Inlay, // 镶嵌 使用scale,等比完整显示图像,使用时,contentsize提前保持比例,比如直接使用SizeMode为RAW
}
Enum(ResizeMode);

@ccclass('ResizeToFullScreen')
@executeInEditMode
export class ResizeToFullScreen extends BaseView {

    @property(ReactiveNumber)
    _resizeMode: ReactiveNumber = ReactiveNumber.DefaultValue(ResizeMode.Stretch);
    @property({ type: ResizeMode })
    get resizeMode(): ResizeMode { return this._resizeMode.value }
    set resizeMode(v: ResizeMode) { this._resizeMode.Value = v; }

    // 扩展, 缩放时, 方便处理半像素对齐时的漏缝现象
    @property
    scaleExtend = 1.001;

    willBind() {
        this._resizeMode.loadValue();
    }

    onLoadView(): void {
        this.log('onLoadView');
        this.adapt();
    }

    useObserves() {
        return [
            gm.fullScreenChangedCommand.subscribe(_ => {
                this.adapt();
            }),
            this._resizeMode.subscribe(_ => {
                this.adapt();
            }),
        ];
    }

    adapt() {
        switch (this.resizeMode) {
            case ResizeMode.Stretch:
                this.doStretch();
                break;
            case ResizeMode.Crop:
                this.doCrop();
                break;
            case ResizeMode.Inlay:
                this.doInLay();
                break;
        }
    }

    // 获取当前的全屏尺寸(与设计分辨率相同的单位)
    getFullscreenSize() {

        let designWidth = view.getDesignResolutionSize().width;
        let designHeight = view.getDesignResolutionSize().height;

        let widthRate = screen.windowSize.width / designWidth;
        let heightRate = screen.windowSize.height / designHeight;
        if (EDITOR) {
            widthRate = this.node.parent.getComponent(UITransform).width / designWidth;
            heightRate = this.node.parent.getComponent(UITransform).height / designHeight;
        }

        if (StringTools.getValueInSearch('edit') == '1') {
            if (widthRate < heightRate) { // 适配了宽度,高度需要加长填满
                return size(designWidth, designHeight / widthRate * heightRate);
            } else if (widthRate > heightRate) { // 适配了高度,宽度需要加长填满
                return size(designWidth / heightRate * widthRate, designHeight);
            }
            return size(designWidth, designHeight);
        } else {
            return size(designWidth, designHeight / widthRate * heightRate);
        }
    }

    doStretch() {
        this.log('doStretch!', CocosTools.path(this.node));
        let fullscreenSize = this.getFullscreenSize();
        fullscreenSize.width *= this.scaleExtend;
        fullscreenSize.height *= this.scaleExtend;
        let transform = this.getComponent(UITransform);
        transform.setContentSize(fullscreenSize);
        // this.node.setScale(1, 1, 1);
        let scale = CocosTools.scaleInHi(this.node);
        this.node.setScale(v3(1 / scale.x, 1 / scale.y, 1 / scale.z));
    }

    doCrop() {
        this.log('doFillFixAspect!', CocosTools.path(this.node));
        let fullscreenSize = this.getFullscreenSize();
        let transform = this.getComponent(UITransform);
        let sprite = this.getComponent(Sprite);
        if (sprite) sprite.sizeMode = Sprite.SizeMode.RAW;
        let currentSize = size(transform.contentSize);
        let widthNeedScale = fullscreenSize.width / currentSize.x;
        let heightNeedScale = fullscreenSize.height / currentSize.y;
        let scale = Math.max(widthNeedScale, heightNeedScale);
        this.node.setScale(v3(1, 1, 1).multiplyScalar(scale * this.scaleExtend));
    }

    doInLay() {
        this.log('doInLay!', CocosTools.path(this.node));
        let fullscreenSize = this.getFullscreenSize();
        let transform = this.getComponent(UITransform);
        let currentSize = size(transform.contentSize);
        let widthNeedScale = fullscreenSize.width / currentSize.x;
        let heightNeedScale = fullscreenSize.height / currentSize.y;
        let scale = Math.min(widthNeedScale, heightNeedScale);
        this.node.setScale(v3(1, 1, 1).multiplyScalar(scale * this.scaleExtend));
        let sprite = this.getComponent(Sprite);
        if (sprite) sprite.sizeMode = Sprite.SizeMode.RAW;
    }

}